﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.Data;
using System.Collections.Specialized;

namespace Projeto2012._1_Desktop_Cabeceira
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        public CookieContainer cookieJar = new CookieContainer();
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                String usuario = textBox1.Text;
                String senha = passwordBox1.Password;
                string uri = "http://localhost:9000/segurancadesktop/authenticate?username=" + usuario + "&password=" + senha;
                HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
                req.CookieContainer = cookieJar;
                req.KeepAlive = false;
                req.Method = "POST";
               
                
                using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
                {
                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), enc);
                    string Response = loResponseStream.ReadToEnd();
                    loResponseStream.Close();
                    resp.Close();
                    if (Response.Equals("Permitido"))
                    {
                        MainWindow w = new MainWindow(cookieJar);
                        w.Show();
                        this.Close();
                        
                    }

                    else
                        MessageBox.Show("Erro de autenticação");
                  
                   

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

    }
}
